﻿#region Imports
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using CommonUtilities.Extensions;
using CommonUtilities.Util;
using DirectoryAnalyzer.Controls.ViewModels;
using DirectoryAnalyzer.Models;
#endregion

namespace DirectoryAnalyzer.Controls
{
    /// <summary>
    /// UserControl to display duplicate files
    /// </summary>
    public partial class DuplicateFileBox : UserControl
    {
        #region Declarations
        // RKD
        //public delegate void DupItemStateChangedEventHandler(object sender, RoutedEventArgs e);
        //
        //public static readonly RoutedEvent PageCloseEvent = EventManager.RegisterRoutedEvent("DupItemStateChangedEventHandler",
        //    RoutingStrategy.Bubble, typeof(DupItemStateChangedEventHandler), typeof(DuplicateFileBox));

        public event RoutedEventHandler DupItemCheckChangedEventHandler;
        public event EventHandler AccordionStateChangedEventHandler;
        //{ // RKD
        //    add { this.AddHandler(PageCloseEvent, value); }
        //    remove { this.RemoveHandler(PageCloseEvent, value); }
        //}
        #endregion

        #region Properties
        private bool isBusy;
        public bool IsBusy
        {
            get { return isBusy; }
            set { isBusy = value; /*bsyMain.IsBusy = isBusy;*/ }
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor
        /// </summary>
        public DuplicateFileBox()
        {
            InitializeComponent();
            this.Language = XmlLanguage.GetLanguage(Utilities.DefaultCulture.Name);
        }
        #endregion

        #region Methods
        /// <summary>
        /// Adds search results for duplicate files
        /// </summary>
        /// <param name="allDuplicates">Duplicate files</param>
        /// <param name="showCustom">Specifies if custom/Accordion should be displayed in result</param>
        internal void AddSearchResults(ObservableCollection<DuplicateFiles> allDuplicates, bool showCustom)
        {
            if (itmctrlResult.DataContext != null)
            {
                //DuplicateFileViewModel vmDuplicateFile = (DuplicateFileViewModel)itmctrlResult.DataContext;
                //vmDuplicateFile.AllDuplicates = allDuplicates.ToObservableCollection();
                ((DuplicateFileViewModel)itmctrlResult.DataContext).AllDuplicates = allDuplicates.ToObservableCollection();
                // RKD itmctrlResult.DataContext = vmDuplicateFile;

                UpdateControlView(showCustom);
            }
        }

        internal void ClearSearchResults()
        {
            if (itmctrlResult.DataContext != null)
                //{
                //    DuplicateFileViewModel vmDuplicateFile = (DuplicateFileViewModel)itmctrlResult.DataContext;
                //    vmDuplicateFile.AllDuplicates = new ObservableCollection<DuplicateFiles>();
                //    RKD itmctrlResult.DataContext = vmDuplicateFile;
                //}
                ((DuplicateFileViewModel)itmctrlResult.DataContext).AllDuplicates = new ObservableCollection<DuplicateFiles>();
        }

        /// <summary>
        /// Updates view of Duplicate File Box
        /// <para>TRUE: Displays in custom view</para>
        /// <para>FALSE: Displays in Accordion</para>
        /// </summary>
        /// <param name="showCustom">Specifies if custom/Accordion should be displayed in result</param>
        internal void UpdateControlView(bool showCustom)
        {
            if (itmctrlResult.DataContext != null)
            {
                // RKD : keep variable as there is twice usage
                DuplicateFileViewModel vmDuplicateFile = (DuplicateFileViewModel)itmctrlResult.DataContext;
                if (vmDuplicateFile != null)
                    if (vmDuplicateFile.ShowCustom != showCustom) vmDuplicateFile.ShowCustom = showCustom;
                // RKD itmctrlResult.DataContext = vmDuplicateFile;
            }
        }
        #endregion

        private void accAccordion_StateChanged(object sender, RoutedEventArgs e)
        {
            OnAccordionStateChanged(sender, e);
        }

        private void OnAccordionStateChanged(object sender, EventArgs e)
        {
            if (this.AccordionStateChangedEventHandler != null) this.AccordionStateChangedEventHandler(sender, e);
        }

        private void chkSelection_CheckedChanged(object sender, RoutedEventArgs e)
        {
            OnDupItemCheckChanged(sender, e);
        }

        private void OnDupItemCheckChanged(object sender, RoutedEventArgs e)
        {
            if (this.DupItemCheckChangedEventHandler != null) this.DupItemCheckChangedEventHandler(sender, e);
        }

        private void mnuDeleteBlock_Click(object sender, RoutedEventArgs e)
        {

        }

        private void mnuCheckAll_Click(object sender, RoutedEventArgs e)
        {

        }

        private void mnuCheckNone_Click(object sender, RoutedEventArgs e)
        {

        }

        private void mnuSuggestDuplicates_Click(object sender, RoutedEventArgs e)
        {

        }

        private void mnuExpandCollapseBlock_Click(object sender, RoutedEventArgs e)
        {

        }

        private void mnuExpandAll_Click(object sender, RoutedEventArgs e)
        {

        }

        private void mnuCollapseAll_Click(object sender, RoutedEventArgs e)
        {

        }

        private void chkSelectionAcc_Checked(object sender, RoutedEventArgs e)
        {

        }

        private void accResult_SelectedItemsChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (accResult.Visibility == Visibility.Visible)
            {
                if (e.NewItems != null)
                {
                    foreach (var item in e.NewItems)
                    {
                        // item expanded
                    }
                }

                if (e.OldItems != null)
                {
                    foreach (var item in e.OldItems)
                    {
                        // item collapsed
                    }
                }

                OnAccordionStateChanged(sender, e);
            }
        }
    }
}
